﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SzCourt.Logic.Common;
using KMFrame.Core;
using SzCourt.Logic.NBL.DBEntities;
using SzCourt.Logic.NBL;
using NBearLite;

namespace SzCourt.Logic.BLL
{
    public class ReporterHome : SzCourtLogic
    {
        /// <summary>
        /// 获取列表信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public PageResult GetReporterHomeList(PageParam condition, string keyword)
        {
            PageResult result = new PageResult();

            WhereClip where = WhereClip.All;
            if (!string.IsNullOrEmpty(keyword))
            {
                where = where && SZCourtColumns.pReporterHomeInfo.Title.Like("%" + keyword + "%");
            }
            result.ResultList = NBLData
                .Select(SZCourtColumns.pReporterHomeInfo)
                .Where(where)
                .SetSelectRange(condition.PageSize, condition.PageSize * (condition.PageIndex - 1), SZCourtColumns.pReporterHomeInfo.ReporterHomeID)
                .OrderBy(SZCourtColumns.pReporterHomeInfo.AddDateTime.Desc)
                .ToList<pReporterHomeInfo>();

            int total = -1;
            if (condition.TotalCount == -1)
            {
                total = GetTotalRecord(SZCourtColumns.pReporterHomeInfo, SZCourtColumns.pReporterHomeInfo.ReporterHomeID, null);
            }
            result.PageIndex = condition.PageIndex;
            result.PageSize = condition.PageSize;
            result.TotalCount = total;
            return result;
        }

        /// <summary>
        /// 获取单条信息
        /// </summary>
        /// <param name="reporterHomeID"></param>
        /// <returns></returns>
        public pReporterHomeInfo GetInfo(int reporterHomeID)
        {
          pReporterHomeInfo result=  NBLData
               .Select(SZCourtColumns.pReporterHomeInfo)
               .Where(SZCourtColumns.pReporterHomeInfo.ReporterHomeID == reporterHomeID)
               .ToSingleObject<pReporterHomeInfo>();

          UpdateSqlSection update = NBLData.Update(SZCourtColumns.pReporterHomeInfo)
             .AddColumn(SZCourtColumns.pReporterHomeInfo.BrowseCount, result.BrowseCount.GetValueOrDefault(0)+1)
             .Where(SZCourtColumns.pReporterHomeInfo.ReporterHomeID == reporterHomeID);

          int ri = update.Execute();

          return result;
        }

        /// <summary>
        /// 新增信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public bool AddInfo(pReporterHomeInfo param)
        {
            bool resutl = false;

            int ri = NBLData.Insert(SZCourtColumns.pReporterHomeInfo)
                .AddColumn(SZCourtColumns.pReporterHomeInfo.AddUserID, param.AddUserID)
                 .AddColumn(SZCourtColumns.pReporterHomeInfo.AddUserName, param.AddUserName)
                .AddColumn(SZCourtColumns.pReporterHomeInfo.AddDateTime, DateTime.Now)
                .AddColumn(SZCourtColumns.pReporterHomeInfo.BrowseCount, 0)
                .AddColumn(SZCourtColumns.pReporterHomeInfo.Title, param.Title)
                .AddColumn(SZCourtColumns.pReporterHomeInfo.Content, param.Content)
                .AddColumn(SZCourtColumns.pReporterHomeInfo.InfoType, param.InfoType)
                .AddColumn(SZCourtColumns.pReporterHomeInfo.IsAudit, 0)
                .ExecuteReturnAutoIncrementID(SZCourtColumns.pReporterHomeInfo.ReporterHomeID);
            if (ri == 1)
                resutl = true;
            return resutl;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public bool EditInfo(pReporterHomeInfo param)
        {
            bool resutl = false;

            UpdateSqlSection update = NBLData.Update(SZCourtColumns.pReporterHomeInfo)
                .AddColumn(SZCourtColumns.pReporterHomeInfo.AddUserID, param.AddUserID)
                 .AddColumn(SZCourtColumns.pReporterHomeInfo.AddUserName, param.AddUserName)
                .AddColumn(SZCourtColumns.pReporterHomeInfo.AddDateTime, DateTime.Now)
                .AddColumn(SZCourtColumns.pReporterHomeInfo.BrowseCount, 0)
                .AddColumn(SZCourtColumns.pReporterHomeInfo.Title, param.Title)
                .AddColumn(SZCourtColumns.pReporterHomeInfo.Content, param.Content)
                .AddColumn(SZCourtColumns.pReporterHomeInfo.InfoType, param.InfoType)
                .AddColumn(SZCourtColumns.pReporterHomeInfo.IsAudit, param.IsAudit)
            .Where(SZCourtColumns.pReporterHomeInfo.ReporterHomeID == param.ReporterHomeID);

            int ri = update.Execute();
            if (ri == 1)
                resutl = true;
            return resutl;
        }


        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="reporterHomeID"></param>
        /// <returns></returns>
        public bool AuditInfo(int reporterHomeID)
        {
            if (reporterHomeID <= 0)
                return false;
            UpdateSqlSection update = NBLData.Update(SZCourtColumns.pReporterHomeInfo)
                .AddColumn(SZCourtColumns.pReporterHomeInfo.IsAudit, 1)
                .Where(SZCourtColumns.pReporterHomeInfo.ReporterHomeID == reporterHomeID);

            int ri = update.Execute();
            if (ri <= 0)
                return false;
            return true;
        }

    }
}
